﻿/***
 * Contains our own custom SlickGrid editors.
 * @module Editors
 * @namespace Slick
 */

(function($) {
	// register namespace
	$.extend(true, window, {
		"Slick": {
			"Editors": {
				"DropDownList": DropDownListEditor,
			}
		}
	});

	function DropDownListEditor(args) {
		var $select;
		var defaultValue;
		var scope = this;

		this.init = function() {
			var optionValues;
			var optionStr;
			if (args.item.options || args.column.options) {
				optionValues = args.item.options || args.column.options;
			} else {
				optionValues = [{ Value: "true", Text: "Yes" }, { Value: "false", Text: "No" }];
			}
			optionStr = "";
			for (var i in optionValues) {
				var value = optionValues[i];
				optionStr += "<OPTION value='" + value.Value + "'>" + value.Text + "</OPTION>";
			}
			$select = $("<SELECT tabIndex='0' class='editor-select'>" + optionStr + "</SELECT>");
			$select.appendTo(args.container);
			$select.focus();
		};

		this.destroy = function() {
			$select.remove();
		};

		this.focus = function() {
			$select.focus();
		};

		this.loadValue = function(item) {
			defaultValue = item[args.column.field];
			$select.val(defaultValue);
		};

		this.serializeValue = function() {
			if (args.item.options || args.column.options) {
				return $select.val();
			} else {
				return ($select.val() == "true");
			}
		};

		this.applyValue = function(item, state) {
			item[args.column.field] = state;
		};

		this.isValueChanged = function() {
			return ($select.val() != defaultValue);
		};

		this.validate = function() {
			return {
				valid: true,
				msg: null
			};
		};

		this.init();
	}

})(jQuery);